﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Omu.AwesomeMvc;
using TCM;
using TCM.Models;

namespace TCM.Controllers{
    public class webpages_Roles1MultiLookupController : Controller
    {
        private TCMContext Db = new TCMContext();
        public ActionResult GetItems(int[] v)
        {
            var items = Db.webpages_Roles.ToList().Where(o => v != null && v.Contains(o.RoleId));
            return Json(items.Select(role => new KeyContent(role.RoleId, role.RoleName.ToString())));
        }
        public ActionResult SearchForm()
        {
            return View();
        }

        public ActionResult Search(string search, int[] selected, int page)
        {
            const int PageSize = 10;
            selected = selected ?? new int[] { };
            search = (search ?? "").ToLower().Trim();

            var items = Db.webpages_Roles.ToList().Where(o => (!selected.Contains(o.RoleId)));

            return Json(new AjaxListResult
            {
                Items = items.Skip((page - 1) * PageSize).Take(PageSize).Select(o => new KeyContent(o.RoleId, o.RoleName.ToString())),
                More = items.Count() > page * PageSize
            });
        }

        public ActionResult Selected(int[] selected)
        {

            var items = Db.webpages_Roles.ToList().Where(o => (selected != null && selected.Contains(o.RoleId)));
            return Json(new AjaxListResult
            {
                Items = items.Select(o => new KeyContent(o.RoleId, o.RoleName.ToString()))
            });
        }
    }

}